/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package argentracker.activeentity;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;

/**
 * briefly description.
 * 
 * extense description.
 * 
 * @author matero (matero _at_ gmail _dot_ com)
 */
public abstract class P<T, AE extends ActiveEntity<AE>> {

    public static final String NO_NAME = null;

    private String name;
    private final T defaultValue;
    private final Class<T> type;
    
    public P(Class<T> propertyType, String propertyName, T propertyDefaultValue) {
        type = propertyType;
        name = propertyName;
        defaultValue = propertyDefaultValue;
    }

    public final Class<T> getType() {
        return type;
    }
    
    public final String getName() {
        return name;
    }

    public final T getDefaultValue() {
        return defaultValue;
    }

    public final void setName(String newName) {
        if (name != NO_NAME)
            throw new IllegalStateException("name already defined");

        name = newName;
    }

    public T in(Entity e) {
        return e.hasProperty(name) ? interpret(e.getProperty(name)) : defaultValue;
    }

    public abstract T interpret(Object value);

    public abstract Object traduct(T value);
    
    public Query.FilterPredicate eq(T value) {
        return new Query.FilterPredicate(name, FilterOperator.EQUAL, value);
    }

    public Query.FilterPredicate ne(T value) {
        return new Query.FilterPredicate(name, FilterOperator.NOT_EQUAL, value);
    }

    public Query.FilterPredicate gt(T value) {
        return new Query.FilterPredicate(name, FilterOperator.GREATER_THAN, value);
    }

    public Query.FilterPredicate ge(T value) {
        return new Query.FilterPredicate(name, FilterOperator.GREATER_THAN_OR_EQUAL, value);
    }

    public Query.FilterPredicate lt(T value) {
        return new Query.FilterPredicate(name, FilterOperator.LESS_THAN, value);
    }

    public Query.FilterPredicate le(T value) {
        return new Query.FilterPredicate(name, FilterOperator.LESS_THAN_OR_EQUAL, value);
    }
    
    public Query.SortPredicate asc() {
        return new Query.SortPredicate(name, SortDirection.ASCENDING); 
    }

    public Query.SortPredicate desc() {
        return new Query.SortPredicate(name, SortDirection.DESCENDING); 
    }
}
